package Server;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Mail {

    private String emailAccountName = "";       //<----Insert the email account used to send emails from
    private String emailAccountPassword = "";   //<---- Insert the email password used by your email account
    private String emailFrom = "";               //<---- Insert the email account used to send emails from
    private String emailTo = "";
    private String subject = "";
    private String welcomeText = "";
    private String endText = "";
    private String clientUsername = "";
    private String clientPassword = "";
    public String smtp = "";
    /**
     * Getters and setters
     */
    public Mail() {
    }

    public String getClientPassword() {
        return clientPassword;
    }

    public void setClientPassword(String clientPassword) {
        this.clientPassword = clientPassword;
    }

    public String getClientUsername() {
        return clientUsername;
    }

    public void setClientUsername(String clientUsername) {
        this.clientUsername = clientUsername;
    }

    public String getSmtp() {
        return smtp;
    }

    public void setSmtp(String smtp) {
        this.smtp = smtp;
    }

    public String getEmailAccountName() {
        return emailAccountName;
    }

    public void setEmailAccountName(String emailAccountName) {
        this.emailAccountName = emailAccountName;
    }

    public String getEmailAccountPassword() {
        return emailAccountPassword;
    }

    public void setEmailAccountPassword(String emailAccountPassword) {
        this.emailAccountPassword = emailAccountPassword;
    }

    public String getEmailFrom() {
        return emailFrom;
    }

    public void setEmailFrom(String emailFrom) {
        this.emailFrom = emailFrom;
    }

    public String getEmailTo() {
        return emailTo;
    }

    public void setEmailTo(String emailTo) {
        this.emailTo = emailTo;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getWelcomeText() {
        return welcomeText;
    }

    public void setWelcomeText(String welcomeText) {
        this.welcomeText = welcomeText;
    }

    public String getEndText() {
        return endText;
    }

    public void setEndText(String endText) {
        this.endText = endText;
    }
    /**
     * Method used to read the properties fil that includes all the settings
     */
    public void readProperties() {
        try {
            String path = System.getProperty("user.dir");
            Properties appProperties = new Properties();
            appProperties.load(new FileInputStream(path + "/settings.properties"));
            setEmailAccountName(appProperties.getProperty("emailAccount"));
            setEmailAccountPassword(appProperties.getProperty("emailPassword"));
            setEmailFrom(appProperties.getProperty("emailFrom"));
            setSmtp(appProperties.getProperty("smtp"));
        } catch (IOException ex) {
            Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
        }
         
    }
    /**
     * Method used to send an e-mail to the new user created
     */
    public void sendMail() {
        try {
            readProperties();
            System.out.println(""+getClientPassword() + "" +getEmailAccountName() +"" +getSmtp());
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", getSmtp());
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            javax.mail.Authenticator authenticator = new javax.mail.Authenticator() {

                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(getEmailAccountName(), getEmailAccountPassword());
                }
            };
            Session sess = Session.getDefaultInstance(props, authenticator);
            sess.setDebug(true);
            Transport transport = sess.getTransport("smtp");
            Message msg = new MimeMessage(sess);
            msg.setFrom(new InternetAddress(getEmailFrom()));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
            msg.setSubject(getSubject());
            msg.setText(getWelcomeText() + "\nBrukernavn: " + getClientUsername() + "\nPassord: " + getClientPassword() + "\n\n" +getEndText());
            transport.connect();
            transport.send(msg);
        } catch (Exception e) {
            System.out.println("err" + e);
        }

    }
}
